package net.office.controller;

import net.office.bean.*;
import net.office.service.BaseDictService;
import net.office.service.LoginService;
import net.office.service.StaffTrainService;
import net.office.utils.TemplateParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StaffTrainController {

    @Autowired
    private StaffTrainService staffTrainService;

    @Autowired
    private BaseDictService baseDictService;

    @Autowired
    private LoginService loginService;

    /*查询个人列表*/
    @RequestMapping("/train/person/list.do")
    public String personList(HttpSession session, Model model){
        StaffInfo staffInfo = (StaffInfo) session.getAttribute("staff");

        List<StaffTrain> staffTrainList = staffTrainService.findAllByStaffId(staffInfo.getStaffId());

        for(int i=0; i<staffTrainList.size(); i++){
            if(staffTrainList.get(i).getStaffTrainApplyResult() != null) {
                staffTrainList.get(i).setApplyResultDict(baseDictService.findOneByDictId(staffTrainList.get(i).getStaffTrainApplyResult()));
            }
            if(staffTrainList.get(i).getStaffTrainResult() != null) {
                staffTrainList.get(i).setTrainResultDict(baseDictService.findOneByDictId(staffTrainList.get(i).getStaffTrainResult()));
            }
        }
        model.addAttribute("staffTrainList", staffTrainList);

        List<BaseDict> applyResultList = baseDictService.findByDictTypeCode(TemplateParam.APPLY_RESULT_CODE);
        List<BaseDict> trainResultList = baseDictService.findByDictTypeCode(TemplateParam.TRAIN_RESULT_CODE);
        model.addAttribute("applyResultList", applyResultList);
        model.addAttribute("trainResultList", trainResultList);

        return "training";
    }

    /*搜索个人列表*/
    @RequestMapping("/train/person/search.do")
    public String personSearch(@RequestParam(value = "staffNo", required = false) String staffNo,
                               @RequestParam(value = "staffName", required = false) String staffName,
                               @RequestParam(value = "staffTrainStartDate", required = false) Date staffTrainStartDate,
                               @RequestParam(value = "staffTrainEndDate", required = false) Date staffTrainEndDate,
                               @RequestParam(value = "staffTrainUnit", required = false) String staffTrainUnit,
                               @RequestParam(value = "staffTrainApplyResult", required = false) Integer staffTrainApplyResult,
                               @RequestParam(value = "staffTrainResult", required = false) Integer staffTrainResult,
                               HttpSession session, Model model){

        StaffTrain staffTrain = new StaffTrain();
        StaffInfo staffInfo = (StaffInfo) session.getAttribute("staff");
        staffTrain.setStaffId(staffInfo.getStaffId());

        if(staffNo!=null && staffNo.trim().length()!=0){
            staffTrain.setStaffNo(staffNo);
        }
        if(staffName!=null && staffName.trim().length()!=0){
            staffTrain.setStaffName(staffName);
        }
        if(staffTrainStartDate!=null){
            staffTrain.setStaffTrainStartDate(staffTrainStartDate);
        }
        if(staffTrainEndDate!=null){
            staffTrain.setStaffTrainEndDate(staffTrainEndDate);
        }
        if(staffTrainUnit!=null && staffTrainUnit.trim().length()!=0){
            staffTrain.setStaffTrainUnit(staffTrainUnit);
        }
        if(staffTrainApplyResult!=null){
            staffTrain.setStaffTrainApplyResult(staffTrainApplyResult);
        }
        if(staffTrainResult!=null){
            staffTrain.setStaffTrainResult(staffTrainResult);
        }

        List<StaffTrain> staffTrainList = staffTrainService.findByStaffTrain(staffTrain);

        for(int i=0; i<staffTrainList.size(); i++){
            if(staffTrainList.get(i).getStaffTrainApplyResult() != null) {
                staffTrainList.get(i).setApplyResultDict(baseDictService.findOneByDictId(staffTrainList.get(i).getStaffTrainApplyResult()));
            }
            if(staffTrainList.get(i).getStaffTrainResult() != null) {
                staffTrainList.get(i).setTrainResultDict(baseDictService.findOneByDictId(staffTrainList.get(i).getStaffTrainResult()));
            }
        }

        model.addAttribute("staffTrainList", staffTrainList);

        List<BaseDict> applyResultList = baseDictService.findByDictTypeCode(TemplateParam.APPLY_RESULT_CODE);
        List<BaseDict> trainResultList = baseDictService.findByDictTypeCode(TemplateParam.TRAIN_RESULT_CODE);
        model.addAttribute("applyResultList", applyResultList);
        model.addAttribute("trainResultList", trainResultList);

        return "training";
    }

    /*查询全部列表*/
    @RequestMapping("/train/all/list.do")
    public String allList(Model model){
        List<StaffTrain> staffTrainList = staffTrainService.findAll();

        for(int i=0; i<staffTrainList.size(); i++){
            if(staffTrainList.get(i).getStaffTrainApplyResult() != null) {
                staffTrainList.get(i).setApplyResultDict(baseDictService.findOneByDictId(staffTrainList.get(i).getStaffTrainApplyResult()));
            }
            if(staffTrainList.get(i).getStaffTrainResult() != null) {
                staffTrainList.get(i).setTrainResultDict(baseDictService.findOneByDictId(staffTrainList.get(i).getStaffTrainResult()));
            }
        }

        List<BaseDict> applyResultList = baseDictService.findByDictTypeCode(TemplateParam.APPLY_RESULT_CODE);
        List<BaseDict> trainResultList = baseDictService.findByDictTypeCode(TemplateParam.TRAIN_RESULT_CODE);
        model.addAttribute("applyResultList", applyResultList);
        model.addAttribute("trainResultList", trainResultList);

        model.addAttribute("staffTrainList", staffTrainList);
        return "training";
    }

    /*搜索全部列表*/
    @RequestMapping("/train/all/search.do")
    public String allSearch(@RequestParam(value = "staffNo", required = false) String staffNo,
                            @RequestParam(value = "staffName", required = false) String staffName,
                            @RequestParam(value = "staffTrainStartDate", required = false) Date staffTrainStartDate,
                            @RequestParam(value = "staffTrainEndDate", required = false) Date staffTrainEndDate,
                            @RequestParam(value = "staffTrainUnit", required = false) String staffTrainUnit,
                            @RequestParam(value = "staffTrainApplyResult", required = false) Integer staffTrainApplyResult,
                            @RequestParam(value = "staffTrainResult", required = false) Integer staffTrainResult,
                            Model model){

        StaffTrain staffTrain = new StaffTrain();

        if(staffNo!=null && staffNo.trim().length()!=0){
            staffTrain.setStaffNo(staffNo);
        }
        if(staffName!=null && staffName.trim().length()!=0){
            staffTrain.setStaffName(staffName);
        }
        if(staffTrainStartDate!=null){
            staffTrain.setStaffTrainStartDate(staffTrainStartDate);
        }
        if(staffTrainEndDate!=null){
            staffTrain.setStaffTrainEndDate(staffTrainEndDate);
        }
        if(staffTrainUnit!=null && staffTrainUnit.trim().length()!=0){
            staffTrain.setStaffTrainUnit(staffTrainUnit);
        }
        if(staffTrainApplyResult!=null){
            staffTrain.setStaffTrainApplyResult(staffTrainApplyResult);
        }
        if(staffTrainResult!=null){
            staffTrain.setStaffTrainResult(staffTrainResult);
        }

        List<StaffTrain> staffTrainList = staffTrainService.findByStaffTrain(staffTrain);

        for(int i=0; i<staffTrainList.size(); i++){
            if(staffTrainList.get(i).getStaffTrainApplyResult() != null) {
                staffTrainList.get(i).setApplyResultDict(baseDictService.findOneByDictId(staffTrainList.get(i).getStaffTrainApplyResult()));
            }
            if(staffTrainList.get(i).getStaffTrainResult() != null) {
                staffTrainList.get(i).setTrainResultDict(baseDictService.findOneByDictId(staffTrainList.get(i).getStaffTrainResult()));
            }
        }

        model.addAttribute("staffTrainList", staffTrainList);

        List<BaseDict> applyResultList = baseDictService.findByDictTypeCode(TemplateParam.APPLY_RESULT_CODE);
        List<BaseDict> trainResultList = baseDictService.findByDictTypeCode(TemplateParam.TRAIN_RESULT_CODE);
        model.addAttribute("applyResultList", applyResultList);
        model.addAttribute("trainResultList", trainResultList);

        return "training";
    }

    /*新增*/
    @RequestMapping("/train/insert.do")
    public String insert(@RequestParam(value = "staffNo", required = false) String staffNo,
                         @RequestParam(value = "staffName", required = false) String staffName,
                         @RequestParam(value = "staffTrainStartDate", required = false) Date staffTrainStartDate,
                         @RequestParam(value = "staffTrainEndDate", required = false) Date staffTrainEndDate,
                         @RequestParam(value = "staffTrainUnit", required = false) String staffTrainUnit,
                         @RequestParam(value = "staffTrainContent", required = false) String staffTrainContent,
                         @RequestParam(value = "staffTrainApplyReason", required = false) String staffTrainApplyReason,
                         @RequestParam(value = "staffTrainApplyResult", required = false) Integer staffTrainApplyResult,
                         @RequestParam(value = "staffTrainResult", required = false) Integer staffTrainResult,
                         HttpSession session, Model model){

        //检查员工是否存在，工号、姓名是否保持一致,...
        StaffInfo staffInfo = loginService.findByStaffNo(staffNo);
        if(staffInfo == null){
            //model.addAttribute("msg", "staff");
            return "redirect:/train/toAdd.do";
        }else if(!staffInfo.getStaffName().equals(staffName)){
            //model.addAttribute("msg", "staff");
            return "redirect:/train/toAdd.do";
        }

        StaffTrain staffTrain = new StaffTrain();

        staffTrain.setStaffId(staffInfo.getStaffId());
        if(staffNo!=null && staffNo.trim().length()!=0){
            staffTrain.setStaffNo(staffNo);
        }
        if(staffName!=null && staffName.trim().length()!=0){
            staffTrain.setStaffName(staffName);
        }
        if(staffTrainStartDate!=null){
            staffTrain.setStaffTrainStartDate(staffTrainStartDate);
        }
        if(staffTrainEndDate!=null){
            staffTrain.setStaffTrainEndDate(staffTrainEndDate);
        }
        if(staffTrainUnit!=null && staffTrainUnit.trim().length()!=0){
            staffTrain.setStaffTrainUnit(staffTrainUnit);
        }
        if(staffTrainContent!=null && staffTrainContent.trim().length()!=0){
            staffTrain.setStaffTrainContent(staffTrainContent);
        }
        if(staffTrainApplyReason!=null && staffTrainApplyReason.trim().length()!=0){
            staffTrain.setStaffTrainApplyReason(staffTrainApplyReason);
        }
        if(staffTrainApplyResult!=null){
            staffTrain.setStaffTrainApplyResult(staffTrainApplyResult);
        }
        if(staffTrainResult!=null){
            staffTrain.setStaffTrainResult(staffTrainResult);
        }

        int res = staffTrainService.insert(staffTrain);

        if(res > 0){
            if(true){
                return "redirect:/train/all/list.do";
            }else{
                return "redirect:/train/person/list.do";
            }

        }

        return "training-add";
    }

    /*修改*/
    @RequestMapping("/train/update.do")
    public String update(@RequestParam(value = "staffTrainId") Integer staffTrainId,
                         @RequestParam(value = "staffNo", required = false) String staffNo,
                         @RequestParam(value = "staffName", required = false) String staffName,
                         @RequestParam(value = "staffTrainStartDate", required = false) Date staffTrainStartDate,
                         @RequestParam(value = "staffTrainEndDate", required = false) Date staffTrainEndDate,
                         @RequestParam(value = "staffTrainUnit", required = false) String staffTrainUnit,
                         @RequestParam(value = "staffTrainContent", required = false) String staffTrainContent,
                         @RequestParam(value = "staffTrainApplyReason", required = false) String staffTrainApplyReason,
                         @RequestParam(value = "staffTrainApplyResult", required = false) Integer staffTrainApplyResult,
                         @RequestParam(value = "staffTrainResult", required = false) Integer staffTrainResult,
                         Model model){

        //检查员工是否存在，工号、姓名是否保持一致,...
        StaffInfo staffInfo = loginService.findByStaffNo(staffNo);
        if(staffInfo == null){
            //model.addAttribute("msg", "staff");
            return "redirect:/staffMail/toAdd.do";
        }else if(!staffInfo.getStaffName().equals(staffName)){
            //model.addAttribute("msg", "staff");
            return "redirect:/staffMail/toAdd.do";
        }

        StaffTrain staffTrain = new StaffTrain();

        staffTrain.setStaffTrainId(staffTrainId);
        staffTrain.setStaffId(staffInfo.getStaffId());
        if(staffNo!=null && staffNo.trim().length()!=0){
            staffTrain.setStaffNo(staffNo);
        }
        if(staffName!=null && staffName.trim().length()!=0){
            staffTrain.setStaffName(staffName);
        }
        if(staffTrainStartDate!=null){
            staffTrain.setStaffTrainStartDate(staffTrainStartDate);
        }
        if(staffTrainEndDate!=null){
            staffTrain.setStaffTrainEndDate(staffTrainEndDate);
        }
        if(staffTrainUnit!=null && staffTrainUnit.trim().length()!=0){
            staffTrain.setStaffTrainUnit(staffTrainUnit);
        }
        if(staffTrainContent!=null && staffTrainContent.trim().length()!=0){
            staffTrain.setStaffTrainContent(staffTrainContent);
        }
        if(staffTrainApplyReason!=null && staffTrainApplyReason.trim().length()!=0){
            staffTrain.setStaffTrainApplyReason(staffTrainApplyReason);
        }
        if(staffTrainApplyResult!=null){
            staffTrain.setStaffTrainApplyResult(staffTrainApplyResult);
        }
        if(staffTrainResult!=null){
            staffTrain.setStaffTrainResult(staffTrainResult);
        }

        int res = staffTrainService.updateByPrimaryKey(staffTrain);

        if(res > 0){

        }

        return "redirect:/train/toEdit.do?staffTrainId="+staffTrainId+"";
    }

    /*删除单条(ajax)*/
    @RequestMapping("/train/deleteOne.do")
    @ResponseBody
    public Object deleteOne(@RequestParam("staffTrainId") Integer staffTrainId){

        int res = staffTrainService.deleteByPrimaryKey(staffTrainId);

        Map<String, String> map = new HashMap<String, String>();
        if(res > 0){
            map.put("res", "1");
        }else{
            map.put("res", "0");
        }
        return map;
    }

    /*跳转到新增页面*/
    @RequestMapping("/train/toAdd.do")
    public String toAdd(Model model){

        List<BaseDict> applyResultList = baseDictService.findByDictTypeCode(TemplateParam.APPLY_RESULT_CODE);
        List<BaseDict> trainResultList = baseDictService.findByDictTypeCode(TemplateParam.TRAIN_RESULT_CODE);
        model.addAttribute("applyResultList", applyResultList);
        model.addAttribute("trainResultList", trainResultList);

        return "training-add";
    }
    /*跳转到修改页面*/
    @RequestMapping("/train/toEdit.do")
    public String toEdit(@RequestParam("staffTrainId") Integer staffTrainId, Model model){
        StaffTrain staffTrain = staffTrainService.findOneByStaffTrainId(staffTrainId);
        model.addAttribute("staffTrain", staffTrain);

        List<BaseDict> applyResultList = baseDictService.findByDictTypeCode(TemplateParam.APPLY_RESULT_CODE);
        List<BaseDict> trainResultList = baseDictService.findByDictTypeCode(TemplateParam.TRAIN_RESULT_CODE);
        model.addAttribute("applyResultList", applyResultList);
        model.addAttribute("trainResultList", trainResultList);

        return "training-edit";
    }

}
